#include <stdio.h>
#include "request.h"
#include "constant.h"
#include <string.h>
void close_socket(int);

void free_request(struct request *req)
{
	if (!req)
		return ;
	
	if (req->fd)
		close_socket(req->fd);
	
	if ( req->transflag ) {
		if (req->erroraccur)
			mysql_rollback(req->mysql);
		else
			mysql_commit(req->mysql);
	}

	if (req->mysql)
		mysql_close(req->mysql);
	
	SAFE_FREE(req->recv_buff);

	req->content = 0;

}


int parse_method(struct request *req)
{
	if (!req->recv_buff)
		return BS_ERROR;

	if ( EOF == sscanf(req->recv_buff, "%s", req->method) )	{
		return BS_ERROR;
	}
	
	return BS_OK;
}

int parse_url(struct request *req)
{
	if (!req->recv_buff)
		return BS_ERROR;

	if ( EOF == sscanf(req->recv_buff, "%*s%s", req->url ) ) {
		return BS_ERROR;
	}

	return BS_OK;
}

int parse_content(struct request *req)
{

	if (!req->recv_buff)
		return BS_ERROR;

	req->content = strstr(req->recv_buff, "\r\n\r\n");

	if ( ! req->content)
		return BS_ERROR;

	req->content += 4;
	if ( !req->content || !*( req->content ) )
		return BS_ERROR;

	return BS_OK;
}
